package com.example.headlines.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.headlines.MainActivity;
import com.example.headlines.R;
import com.example.headlines.api.Api;
import com.example.headlines.api.ApiConfig;
import com.example.headlines.api.TtitCallback;
import com.example.headlines.databinding.ActivityLoginBinding;
import com.example.headlines.databinding.ActivityRegisterBinding;
import com.example.headlines.util.StringUtils;

import java.util.HashMap;

public class RegisterActivity extends BaseActivity {
    private ActivityRegisterBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        binding = ActivityRegisterBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account= binding.etAccount.getText().toString().trim();
                String pwd= binding.etPwd.getText().toString().trim();
                register(account,pwd);
            }
        });
    }

    @Override
    protected void initData() {

    }

    private void register(String account, String pwd) {
        if (StringUtils.isEmpty(account)) {
            showToast("请输入账号");
            return;
        }
        if (StringUtils.isEmpty(pwd)) {
            showToast("请输入密码");
            return;
        }
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("mobile", account);
        params.put("password", pwd);
        Api.config(ApiConfig.REGISTER, params).postRequest(this,new TtitCallback() {
            @Override
            public void onSuccess(final String res) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        showToast("注册成功");
                    }
                });
                navigateTo(MainActivity.class);
            }

            @Override
            public void onFailure(Exception e) {
                Log.e("onFailure", e.toString());
            }
        });
    }
}